﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace WebApplication1
{
    public partial class Signup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Bind site to drop down
                SiteManagement sitemgm = new SiteManagement();
                List<Site> sites = sitemgm.GetAllSite();

                dropdownSite.DataSource = sites;
                dropdownSite.DataTextField = "name";
                dropdownSite.DataValueField = "id";
                dropdownSite.DataBind();
                dropdownSite.SelectedIndex = 0;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            fos_User newUser = new fos_User();
            UserManagement usermgt = new UserManagement();
            newUser.Name = txtName.Text;
            newUser.WWID = txtWWID.Text;
            newUser.Phone = txtPhone.Text;
            
            newUser.Password = Security.EncodePassword(txtPassword.Text);
            newUser.Email = txtEmail.Text;
            string roleInfo = "2"; // Employee
            string siteInfo = dropdownSite.SelectedValue;

            newUser.IsDeleted = 0;
            newUser.isActive = false;

            try
            {
                usermgt.SaveUser(newUser, Security.EncodePassword(txtRetypePassword.Text), false);
                usermgt.UpdateUser(newUser, siteInfo, roleInfo, "");
                usermgt.SendActivationLink(newUser);
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Thank you for registering at our site. Please check your email account for verification link!";

            }
            catch (Exception ex)
            {
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = ex.Message;
            }
            

        }
    }
}